package com.heyun.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heyun.bean.HeyunDept;
import com.heyun.bean.HeyunEmp;
import com.heyun.bean.HeyunRole;
import com.heyun.service.HeyunDeptService;
import com.heyun.service.HeyunEmpService;
import com.heyun.service.HeyunRoleService;

/**
 * @author 成栋梁
 *
 */
@RequestMapping("/emp")
@Controller
public class EmpController {
  @Autowired
  private HeyunEmpService hyes;
  @Autowired
  private HeyunDeptService hyds;
  @Autowired
  private HeyunRoleService hyrs;
  @RequestMapping("/getAllEmp")
  @ResponseBody
  public Map getAllEmp(String pageNum){
	  Map map=new HashMap();
	  //int totalPages=hyes.listEmp().size();
	  Page page=PageHelper.startPage(Integer.valueOf(pageNum), 2);
	  List<HeyunEmp> listEmp = hyes.listEmp();
	  for (HeyunEmp l:listEmp) {
		System.out.println("============@@#"+l.getHye());
	}
	  int totals=(int) page.getPages();
	  System.out.println("*********总页数"+totals);
	  map.put("empList", listEmp);
	  map.put("totalPages", totals);
	  return map;
  }
  @RequestMapping("/getDeptRole")
  @ResponseBody
  public Map getDeptRole(){
	  Map map=new HashMap();
	  List<HeyunDept> listAllDept = hyds.listAllDept();
	  map.put("deptList",listAllDept);
	  return map;
  }
  @RequestMapping("/getRoleName")
  @ResponseBody
  public Map getRoleName(String deptId){
	  Map map=new HashMap();
	   List<HeyunRole> listDeptRole = hyrs.listDeptRole(Integer.valueOf(deptId));
	  map.put("roleList", listDeptRole);
	   return map;
  }
  @RequestMapping("getEmpMessage")
  @ResponseBody
  public Map getEmpMessage(String empId){
	  Map map=new HashMap();
       HeyunEmp emp = hyes.getEmp(Integer.valueOf(empId));
       map.put("emp", emp);
       List<HeyunDept> listAllDept = hyds.listAllDept();
       map.put("deptList", listAllDept);
	  return map;
  }
  @RequestMapping("/saveEmp")
  @ResponseBody
  public Map saveEmp(HeyunEmp hye ,HttpServletRequest request){  
	  Map map=new HashMap();
	  HeyunEmp user=(HeyunEmp) request.getSession().getAttribute("user");
	  System.out.println(user);
	  int c=user.getEmpId();
	  hye.setMgr(c);
	  hye.setPassword("123456");
	  Date date=new Date();
	  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  hye.setHireDate(sdf.format(date));
	  int b=hyes.listEmp().size()+1;
	  hye.setEmpLoginName("Heyun00"+b);
	 int a= hyes.insertEmp(hye);
	 if(a>0){
		 map.put("message", "添加成功");
	 }else{
		 map.put("message", "系统出错，请重新操作");
	 }
	  return map;
  }
  @RequestMapping("/updateEmp")
  @ResponseBody
  public Map updateEmp(HeyunEmp hye){
	  Map map=new HashMap();
	  int a=hyes.updateEmp(hye);
	  if(a>0){
			 map.put("message", "更新成功");
		 }else{
			 map.put("message", "系统出错，请重新操作");
		 }
	  return map;
  }
  @RequestMapping("/deleteEmp")
  @ResponseBody
  public Map deleteEmp(String empId){
	  Map map=new HashMap();
	  
	  int a=hyes.removeEmp(Integer.valueOf(empId));
	  
	  if(a>0){
			 map.put("message", "删除成功");
		 }else{
			 map.put("message", "系统出错，请重新操作");
		 }
	  
	  return map;
  }
  @RequestMapping("/searchEmp")
  @ResponseBody
  public Map searchEmp(String empRealName,String gthireDate,String lthireDate,String deptId,String empRemarks,String curr){
	  Map map=new HashMap();
	  if(empRealName!=null && empRealName != ""){
		  map.put("empRealName", empRealName);
	  }
	  if(gthireDate !=null && gthireDate!=""){
		  map.put("gthireDate", gthireDate);
	  }
	  if(lthireDate !=null && lthireDate !=""){
		  map.put("lthireDate", lthireDate);
	  }
	  if(deptId !=null && deptId!=""){
		  map.put("deptId", deptId);
	  }
	  if(empRemarks !=null && empRemarks !=""){
		  map.put("empRemarks", empRemarks);
	  }
	  Page page=PageHelper.startPage(Integer.valueOf(curr), 2);
	  List<HeyunEmp> findEmplist = hyes.findEmplist(map);
	   int totals=page.getPages();
	   Map map1=new HashMap();
	   map1.put("empList", findEmplist);
	   map1.put("totalPages", totals);
	  return map1;
  }
  
  @RequestMapping("/updateState")
  @ResponseBody
  public Map updateState(String empId){
	  Map map1=new HashMap();
	  
	  Map map=new HashMap();
	  
	  HeyunEmp hye= hyes.getEmp(Integer.valueOf(empId));
	  int b=hye.getState();
	  int state;
	  if(b==0){
		  state=1;
	  }else{
		  state=0;
	  }
	  
	  map1.put("empId", empId);
	  
	  map1.put("state", state);
	  
	 int a= hyes.updateEmpState(map1);
	 if(a>0){
		 map.put("message","更新成功");
	 }else{
		 map.put("message", "系统出错，请重新操作");
	 }
	  return map;
  }
}
